/**
 * @(#)CodeExamples.java
 * read_From_File_Example
 * write_To_File_Example
 * idGenerator
 * @DS_092 
 * 
 */

import java.util.* ;
import java.io.* ;
public class CodeExamples{

   /*public static void main(String[] args){
   	
 	    //read_From_File_Example("input1.dat");
 	    //read_From_File_Example("input2.dat");
 	    //read_From_File_Example("input3.dat");
  		//write_To_File_Example("e.dat");
  		//String[] arrayOfGeneratedId = idGenerator(1000);
  				
   	
   }*/
   
   
   public static void read_From_File_Example(String inputFilename){
       	System.out.println("Reading from file " + inputFilename);
        int n = 0;
		int m = 0;
		String[] id = null;
        try {
            File inFile = new File(inputFilename);
            FileReader ifr = new FileReader(inFile);
            BufferedReader ibr= new BufferedReader(ifr) ;

            String line = "" ;
            while (line != null )
            {
                line = ibr.readLine() ;
                if (line != null)
                {
                    StringTokenizer st = new StringTokenizer(line,"=") ;
                    String attr= st.nextToken().trim() ;
                    String value= st.nextToken().trim() ;

                    // **********************************************************
                    //  THIS IS WHERE YOU HANDLE YOUR DATA

                    // For example:

                    if (attr.equals("N")) {
                        n = Integer.valueOf(value).intValue();
                    }
                    
                    if (attr.equals("M")) {
                        m = Integer.valueOf(value).intValue();
                    }
                    

                    if (attr.equals("ID")) {
                        id = value.split(",");
                    }

                    // **********************************************************


                }
            }

            ibr.close();
            ifr.close();
        }

        catch(Exception e) {
            System.out.println( "Error \""+ e.toString()+ "\" on file "+inputFilename);
            e.printStackTrace() ;
            System.exit(-1) ;      //brutally exit the program
        }




        //Example test of inputs:

        System.out.println("N = "+n);
		System.out.println("M = "+m);
        if (id != null) {
        	
            System.out.print("Id: ");
            for (int i = 0; i < id.length; i++) {
                System.out.print(id[i]+" ");
            }
            System.out.println();
        }



    }


    private static void write_To_File_Example(String outputFilename) {

        System.out.println("Writing to file " + outputFilename);
        int n = 100;
        int m = 10;

        try {
            File outFile = new File(outputFilename);
            FileWriter ofw = new FileWriter(outFile);

            //Writing to file
            String nStr = Integer.toString(n);
          	ofw.write("N="+nStr+"\r\n");
          	ofw.write("M="+Integer.toString(m)+"\r\n");
            ofw.close();
        }
        catch (Exception e) {
            System.out.println( "Error \""+ e.toString()+ "\" on file "+outputFilename);
            e.printStackTrace() ;
            System.exit(-1) ;      //brutally exit the program
        }

    }
    
    private static String[] idGenerator(int numOfId)
    {
    	final int NUMBER_OF_DIGITS = 9;
    	Random random = new Random();
    	String[] ans = new String[numOfId];
    	for (int i = 0 ; i < numOfId ; i++)
    	{
    		ans[i] = "";
    		for (int j = 0 ; j < NUMBER_OF_DIGITS ; j++ )
    		{
    			ans[i]+= random.nextInt(NUMBER_OF_DIGITS+1);	
    		}
    	}
    	
    	return ans;
    	
    	
    	
    	
    }
}
    
  